package com.zrrd.jt_admin.user.controller;


import com.zrrd.jt_admin.common.dto.StatusDto;
import com.zrrd.jt_admin.common.entity.User;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.page.UserPageVo;
import com.zrrd.jt_admin.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanjl
 * @since 2024-06-30
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/{page}")
    @ResponseBody
    public ResponseResult findByPage(@PathVariable("page") int page,
                                     @RequestParam(required = false) Integer limit,
                                     @RequestParam(required = false) String keyword,
                                     @RequestParam(required = false) Integer userLevelId) {
        UserPageVo userPageVo=userService.finByPage(page,limit,keyword,userLevelId);
        return ResponseResult.queryResponse(userPageVo);
    }

    @ApiOperation(value = "添加", notes = "添加用户信息")
    @PostMapping
    @ResponseBody
    public ResponseResult add(@RequestBody User user) {
        return ResponseResult.addResponse(userService.save(user));
    }

    @ApiOperation(value = "修改", notes = "修改用户信息")
    @PutMapping
    @ResponseBody
    public ResponseResult update(@RequestBody User user) {
        return ResponseResult.updateResponse(userService.updateById(user));
    }

    @ApiOperation(value = "修改状态", notes = "根据id修改状态")
    @PutMapping("/{id}/updateStatus")
    @ResponseBody
    public ResponseResult setStatus(@PathVariable("id") Integer id,@RequestBody StatusDto statusDto) {
        Boolean bStatus=statusDto.getStatus()==1?true:false;

        boolean flag = userService.lambdaUpdate()//使用lambda形式的修改
                .eq(User::getId, id) //比较id
                .set(User::getStatus, bStatus)//进行修改
                .update();//执行修改
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "删除", notes = "根据id删除")
    @DeleteMapping("/{id}/delete")
    @ResponseBody
    public ResponseResult remove(@PathVariable("id") Integer id) {
        return ResponseResult.removeResponse(userService.removeById(id));
    }

}

